package edu.zju.gis.dbfg.model.statistic;

import edu.zju.gis.dbfg.common.exception.ModelFailedException;
import edu.zju.gis.dbfg.model.util.CrsPick;
import edu.zju.gis.dbfg.model.util.CrsPick2;
import edu.zju.gis.dbfg.model.util.GeoItem;
import edu.zju.gis.dbfg.model.util.WriteToFile;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaSparkContext;
import org.geotools.referencing.CRS;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileWriter;
import java.util.Map;

public class LUChangeStatistic23 {
    private final static Logger logger = LoggerFactory.getLogger(LUChangeStatistic23.class);
    public static void main(String[] args) throws Exception {
        logger.info("Setup Spark Context");
        SparkConf conf = new SparkConf();
        //conf.setMaster("local[1]");
        //conf.setAppName("lalala");
        JavaSparkContext jsc = new JavaSparkContext(conf);

        logger.info("Setup arguments");
        LUChangeStatistic23Args mArgs = LUChangeStatistic23Args.initArgs(args);
        if (mArgs == null) {
            String msg = "init args failed, exit";
            logger.error(msg);
            throw new ModelFailedException(LUChangeStatistic23.class, "main()", msg, args);
        }
        CoordinateReferenceSystem crs1 = CRS.parseWKT(CrsPick.pickByName(mArgs.getCrs1()));
        CoordinateReferenceSystem d3crs2 = CRS.parseWKT(CrsPick.pickByName(mArgs.getD3crs2()));

        String output = mArgs.getOutput();
        String input1 = mArgs.getInput1();
        String type1 = mArgs.getType1();

        String d3input2 = mArgs.getD3input2();
        String d3type2 = mArgs.getD3type2();
        String d3zldwdm2 = mArgs.getD3zldwdmIndex2();
        String d3dlbm2 = mArgs.getD3dlbmIndex2();
        String d3area2 = mArgs.getD3areaIndex2();
        String d3jarea2 = mArgs.getD3jareaIndex2();
        String d3kcdlbm2 = mArgs.getD3kcdlbmIndex2();

        Map<String,GeoItem> result=LUAreaStatistic.areaStatistic( jsc, crs1, d3crs2,  input1, type1, d3input2 , d3type2, d3zldwdm2, d3dlbm2, d3area2, d3jarea2, d3kcdlbm2);
        File file = new File(output);
        if (file.exists()) {
            file.delete();
        }
        FileWriter writer = new FileWriter(file);
        writer.write("三调数据统计结果:\n");

        WriteToFile.writeToFile(writer,result);





        CoordinateReferenceSystem crs2 = CRS.parseWKT(CrsPick.pickByName(mArgs.getD2crs2()));
        CoordinateReferenceSystem crs3 = CRS.parseWKT(CrsPick2.pickByName(mArgs.getD2crs3()));
        CoordinateReferenceSystem crs4 = CRS.parseWKT(CrsPick.pickByName(mArgs.getD2crs4()));



        String input2 = mArgs.getD2input2();
        String type2 = mArgs.getD2type2();
        String zldwdm2 = mArgs.getD2zldwdmIndex2();
        String dlbm2 = mArgs.getD2dlbmIndex2();
        String area2 = mArgs.getD2areaIndex2();
        String jarea2 = mArgs.getD2jareaIndex2();
        String kcdlbm2 = mArgs.getD2kcdlbmIndex2();
        String kcdlxs2 = mArgs.getD2kcdlxsIndex2();
        String tbbh2 = mArgs.getD2tbbhIndex2();
        String tbybh2 = mArgs.getD2tbybhIndex2();
        String xzdwmj2 = mArgs.getD2xzdwmjIndex2();
        String lxdwmj2 = mArgs.getD2lxdwmjIndex2();



        String input3 = mArgs.getD2input3();
        String type3 = mArgs.getD2type3();
        String dlbm3 = mArgs.getD2dlbmIndex3();
        String kctbdwdm13 = mArgs.getD2kctbdwdm1Index3();
        String kctbdwdm23 = mArgs.getD2kctbdwdm2Index3();
        String kctbbh13 = mArgs.getD2kctbbh1Index3();
        String kctbbh23 = mArgs.getD2kctbbh2Index3();
        String kcbl3 = mArgs.getD2kcblIndex3();
        String cd3 = mArgs.getD2cdIndex3();
        String kd3 = mArgs.getD2kdIndex3();


        String input4 = mArgs.getD2input4();
        String type4 = mArgs.getD2type4();
        String dlbm4 = mArgs.getD2dlbmIndex4();
        String zldwdm4 = mArgs.getD2zldwdmIndex4();
        String zltbbh4 = mArgs.getD2zltbbhIndex4();
        String mj4 = mArgs.getD2mjIndex4();

        Map<String,GeoItem> results= LUAreaStatistic2D.areaStatistic2d(jsc, crs1, crs2, crs3, crs4,  input1, type1, input2, type2, zldwdm2, dlbm2, area2, jarea2, kcdlbm2, kcdlxs2, tbbh2, tbybh2, xzdwmj2, lxdwmj2, "XIAN", input3, type3, dlbm3, kctbdwdm13, kctbdwdm23, kctbbh13, kctbbh23, kcbl3, cd3, kd3, input4, type4, dlbm4, zldwdm4, zltbbh4, mj4);
        writer.write("\n二调数据统计结果:\n");
        WriteToFile.writeToFile(writer,results);
        writer.close();

    }
}
